package com.xianzhiOA.controller;

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzRole;
import com.xianzhiOA.service.XzRoleService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xianzhiOA.service.XzEmpService;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestParam;

import javax.management.relation.Role;
import java.util.*;

/**
 * Created by Administrator on 2017/4/10.
 */
@Controller
@RequestMapping("/XzEmp")
public class XzEmpController extends BaseController {

    @Autowired
    XzEmpService xzEmpService;
   @Autowired
   XzRoleService xzRoleService;

    //跳转到添加页面
    @ResponseBody
        @RequestMapping("/ToAddEmpPage.emp")
    public ModelAndView ToAddEmpPage() {
        ModelAndView mv = new ModelAndView("backEnd/empEdit");
        XzEmp xzEmp = new XzEmp();
        XzRole xzRole=new XzRole();
        List<XzRole> RoleList=xzRoleService.selRoleAll(xzRole);

        mv.addObject("xzEmp", xzEmp);
        mv.addObject("RoleList",RoleList);
        mv.addObject("editType","add");
        return mv;
    }


    //添加员工
    @RequestMapping("/addEmp.emp")
    public ModelAndView addEmp(@Validated({XzEmp.F1.class, XzEmp.F2.class}) XzEmp xzEmp, BindingResult result) {
        // ModelAndView mv = new ModelAndView("redirect:selEmpAll");
        ModelAndView mv = new ModelAndView("redirect:selEmpAll.emp");
        //角色的Select回显
        XzRole xzRole=new XzRole();
        List<XzRole> RoleList=xzRoleService.selRoleAll(xzRole);
        mv.addObject("RoleList",RoleList);
//        Long roleid= Long.parseLong(xzEmp.getEmpRole());
//        xzRole.setRoleId(roleid);
        xzEmp.setRole(xzRole);

        mv.addObject("xzEmp",xzEmp);

        if (xzEmp.getEmpTel() != null && !"".equals(xzEmp.getEmpTel())) {
            boolean   checkTelRes = xzEmpService.checkTelNO(xzEmp.getEmpTel());

            if (!checkTelRes) {
                mv.addObject("TelMessge", "请正确输入手机号");
                mv.setViewName("backEnd/empEdit");
                mv.addObject("editType","add");
                return mv;
            }
        }

        if (result.hasErrors()) {
//            验证失败
//            mv = new ModelAndView("backEnd/empEdit");
            mv.setViewName("backEnd/empEdit");
            mv.addObject("editType","add");
        } else {
            try {
                if (xzEmp.getEmpTel() != null) {
                }
                xzEmpService.insert(xzEmp);
                mv.addObject("msg", "添加成功");
            } catch (Exception e) {
                e.printStackTrace();
                mv.addObject("msg", "添加失败");
            }
        }



        return mv;
    }

    //跳转到主页面
    @RequestMapping("/toEmpShowPage.emp")
    public ModelAndView toEmpShowPage() {
        // ModelAndView mv = new ModelAndView("empShow");
        ModelAndView mv = new ModelAndView("redirect:selEmpAll.emp");
        return mv;
    }


    //全部显示员工(分页显示)
    @RequestMapping("/selEmpAll.emp")
    public ModelAndView selEmpAll(XzEmp xzEmp, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows, Date begin, Date end) {
        ModelAndView mv = new ModelAndView("backEnd/empShow");
        PageBean<XzEmp> pageBean = xzEmpService.selAll(xzEmp, begin, end, page, rows);
        List<XzEmp> xzEmpList = pageBean.getList();
        XzRole xzRole=new XzRole();
        List<XzRole> RoleList=xzRoleService.selRoleAll(xzRole);
        mv.addObject("RoleList",RoleList);
        mv.addObject("EmpList", pageBean.getList());
        mv.addObject("Emp", xzEmp);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getSize());
        mv.addObject("total", pageBean.getTotal());
        return mv;
    }


    //编辑员工(并跳转到修改页面)
    @RequestMapping("/editEmp.emp")
    public ModelAndView editEmp(long empId) {
        ModelAndView mv = new ModelAndView("backEnd/empEdit");
        XzEmp xzEmp = xzEmpService.findById(empId);

        XzRole xzRole=new XzRole();
        List<XzRole> RoleList=xzRoleService.selRoleAll(xzRole);
        mv.addObject("RoleList",RoleList);
        mv.addObject("xzEmp", xzEmp);
        mv.addObject("editType","update");
        return mv;
    }


    //修改员工
    @RequestMapping("updateEmp.emp")
    public ModelAndView updateEmp(@Validated({XzEmp.F1.class})XzEmp xzEmp, BindingResult result) {
        ModelAndView mv = new ModelAndView("redirect:selEmpAll.emp");
        //    ModelAndView mv=new ModelAndView("editEmp");

        //角色的Select回显
        XzRole xzRole=new XzRole();
        List<XzRole> RoleList=xzRoleService.selRoleAll(xzRole);
        mv.addObject("RoleList",RoleList);
//        Long roleid= Long.parseLong(xzEmp.getEmpRole());
//        xzRole.setRoleId(roleid);
        xzEmp.setRole(xzRole);
        mv.addObject("xzEmp",xzEmp);

        if (result.hasErrors()) {
//            验证失败
            mv.setViewName("backEnd/empEdit");
            mv.addObject("editType","update");
        } else if (xzEmp.getEmpTel() != null && !"".equals(xzEmp.getEmpTel())) {
            boolean   checkTelRes = xzEmpService.checkTelNO(xzEmp.getEmpTel());

            if (!checkTelRes) {
                mv.addObject("TelMessge", "请正确输入手机号");
                mv.setViewName("backEnd/empEdit");
                mv.addObject("editType","update");
                return mv;
            }
            else {

                xzEmpService.updateByPrimaryKey(xzEmp);
                mv.addObject("msg", "修改成功");

            }
        }

        return mv;
    }

    //删除
    @RequestMapping("deleteEmp.emp")
    public ModelAndView deleteEmp(long empId) {
        ModelAndView mv = new ModelAndView("redirect:selEmpAll.emp");
        XzEmp xzEmp = new XzEmp();
        Date newtime = new Date();
        xzEmp.setEmpId(empId);
        xzEmp.setDeleteTime(newtime);
        xzEmpService.phoneyDeleteByPrimaryKey(xzEmp);
        return mv;
    }

    //给员工分配角色的全查
    @RequestMapping("roleAllotShow.emp")
    public ModelAndView roleAllotShow(XzEmp xzEmp, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows, Date begin, Date end){
       ModelAndView mv=new ModelAndView("backEnd/roleAllot");
        PageBean<XzEmp> pageBean = xzEmpService.selAll(xzEmp, begin, end, page, rows);
        List<XzEmp> xzEmpList = pageBean.getList();
        XzRole xzRole=new XzRole();
        List<XzRole> RoleList=xzRoleService.selRoleAll(xzRole);
        mv.addObject("RoleList",RoleList);
        mv.addObject("EmpList", pageBean.getList());
        mv.addObject("Emp", xzEmp);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getSize());
        mv.addObject("total", pageBean.getTotal());
       return mv;
    }

    //跳到给员工分配角色的页面
    @RequestMapping("toRoleAllotPage.emp")
    public ModelAndView toRoleAllotPage(Long empId){
        ModelAndView mv=new ModelAndView("backEnd/roleAllot");


        return mv;
    }




    ////////////////////////////////////////前台方法/////////////////////////////////////
    //根据用户名查询
    @ResponseBody
    @RequestMapping("selectByName.do")
    public Map<String, Object> selectByName(String empName){
        Map<String,Object> hashMap=new HashMap<>();
        List<XzEmp> xzEmpList=xzEmpService.selByname(empName);
        hashMap.put("xzEmpInfo",xzEmpList);
        return hashMap;
    }
    @RequestMapping("updateEmpRole.emp")
    public ModelAndView updateEmpRole(XzEmp emp){
        ModelAndView mv = new ModelAndView("redirect:roleAllotShow.emp");
        xzEmpService.updateByPrimaryKey(emp);
        return mv;

    }

}